package com.sgb.kjwl.view.ui.fragment.shop;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentShopTdChildIntroBinding;
import com.sgb.kjwl.di.shopping.DaggerShoppingFragComponent;
import com.sgb.kjwl.di.shopping.ShoppingFragModule;
import com.sgb.kjwl.model.entity.ShopTeamInfoEntity;
import com.sgb.kjwl.view.ui.adapter.self.packers.FlowLayoutAdapter0;
import com.sgb.link.view.widget.ExpandedTextView;
import com.sgb.kjwl.viewmodel.shopping.ShopTdIntroModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.StringUtil;

import javax.inject.Inject;

/**
 * Created by WYJ on 2020/8/31.
 * 企业店铺 工队简介
 */
public class ShopTdIntroFragment extends AppLazyFragment {
    public static final String SHOP_ID = "shop_id";
    public static final String CHANNEL_ID = "channel_id";

    @Inject
    ShopTdIntroModel mModel;
    private FragmentShopTdChildIntroBinding mBind;

    private String mShopId;
    private String mChannelId;


    private FlowLayoutAdapter0 mAdapter1;
    private FlowLayoutAdapter0 mAdapter2;

    public static ShopTdIntroFragment newInstance(String shopId, String id) {
        ShopTdIntroFragment fragment = new ShopTdIntroFragment();
        Bundle bundle = new Bundle();
        bundle.putString(SHOP_ID, shopId);
        bundle.putString(CHANNEL_ID, id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBind = DataBindingUtil.inflate(inflater, R.layout.fragment_shop_td_child_intro, container, false);
        return mBind.getRoot();
    }


    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerShoppingFragComponent.builder()
                .appComponent(appComponent)
                .shoppingFragModule(new ShoppingFragModule(ShopTdIntroFragment.this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mShopId = getArguments().getString(SHOP_ID);
        mChannelId = getArguments().getString(CHANNEL_ID);

    /*    mAdapter1 = new FlowLayoutAdapter0(getActivity(), new ArrayList<>());
        mBind.rvBusiness.setLayoutManager(new FlowLayoutManager());
        mBind.rvBusiness.addItemDecoration(new MyItemDecoration());
        mBind.rvBusiness.setAdapter(mAdapter1);*/

       /* mAdapter2 = new FlowLayoutAdapter0(getActivity(), new ArrayList<>());
        mBind.rvArea.setLayoutManager(new FlowLayoutManager());
        mBind.rvArea.addItemDecoration(new MyItemDecoration());
        mBind.rvArea.setAdapter(mAdapter2);*/
    }

    @Override
    protected void onLazyLoad() {
        mModel.getData(mShopId, mChannelId);
    }

    public void showEmpty() {
        mBind.emptyView.setVisibility(View.VISIBLE);
    }

    public void updateViews(ShopTeamInfoEntity shopTeamInfoEntity) {
        mBind.emptyView.setVisibility(View.GONE);
        mBind.setViewModel(shopTeamInfoEntity);
        if (!TextUtils.isEmpty(shopTeamInfoEntity.getPhoneNumber())){
            mBind.tvPhone.setText(StringUtil.getPhoneToName(shopTeamInfoEntity.getPhoneNumber()));
        }
        mBind.teamName.setText(shopTeamInfoEntity.getTeamName()+"");
        switch (shopTeamInfoEntity.getTeamType()){
            case 0:
                mBind.tvTeamType.setText("施工队");
                break;
            case 1:
                mBind.tvTeamType.setText("班组工队");
                break;
            case 2:
                mBind.tvTeamType.setText("承包商");
                break;
        }
        mBind.tvTeamNum.setText(shopTeamInfoEntity.getTeamSize() + "人");
        if (TextUtils.isEmpty(shopTeamInfoEntity.getTeamSize()) || TextUtils.equals("0", shopTeamInfoEntity.getTeamSize())) {
            mBind.llTeamSize.setVisibility(View.GONE);
        }
        String categoryName = shopTeamInfoEntity.getCategoryName();
        if(!TextUtils.isEmpty(categoryName)){
           /* String[] split = categoryName.split(",");
            mAdapter1.setDatas(Arrays.asList(split));*/
            mBind.tiBusinessText.setText(categoryName.replaceAll(",","、"));
        }else {
            mBind.tiBusinessText.setText("");
        }

     /*   String address = shopTeamInfoEntity.getServiceAreaName();
        if(!TextUtils.isEmpty(address)){
            String[] split = address.split("、");
            mAdapter2.setDatas(Arrays.asList(split));
        }*/
        String address = shopTeamInfoEntity.getServiceAreaName();
        if (!TextUtils.isEmpty(address)) {
            StringBuilder builder = new StringBuilder();
            String[] split = address.split("、");
            for (int i = 0; i < split.length; i++) {
                builder.append(split[i] + "/");
            }
            mBind.tvServiceArea.setText(builder.substring(0, builder.length() - 1));
            //mAdapter2.setDatas(Arrays.asList(split));
        }
        new ExpandedTextView().showText(mBind.tvBasic, mBind.tvBasicExpand);
        new ExpandedTextView().showText(mBind.tvHistory, mBind.tvHistoryExpand);
        new ExpandedTextView().showText(mBind.tvStructure, mBind.tvStructureExpand);
        new ExpandedTextView().showText(mBind.tvIdea, mBind.tvIdeaExpand);
    }
}
